package com.ttit.myapp.activity;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.ttit.myapp.R;
import com.ttit.myapp.api.Api;
import com.ttit.myapp.api.ApiConfig;
import com.ttit.myapp.api.TtitCallback;
import com.ttit.myapp.entity.LoginResponse;
import com.ttit.myapp.util.AppConfig;
import com.ttit.myapp.util.StringUtils;

import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


public class LoginActivity extends BaseActivity {

    private EditText etAccount;
    private EditText etPwd;
    private Button btnLogin;

    @Override
    protected int initLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        etAccount = findViewById(R.id.et_account);
        etPwd = findViewById(R.id.et_pwd);
        btnLogin = findViewById(R.id.btn_login);
    }

    @Override
    protected void initData() {
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String account = etAccount.getText().toString().trim();
                String pwd = etPwd.getText().toString().trim();
                navigateToWithFlag(HomeActivity.class,
                        Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
//                login(account, pwd);
            }
        });
    }

    private void login(String account, String pwd){
        if(StringUtils.isEmpty(account)) {
            showToast("请输入账号");
            return;
        }
        if(StringUtils.isEmpty(pwd)){
            showToast("请输入密码");
            return;
        }
        HashMap<String, Object> params = new HashMap<>();
        params.put("mobile", account);
        params.put("password", pwd);
        Api.config(ApiConfig.LOGIN, params).postRequest(new TtitCallback() {
            @Override
            public void onSuccess(String res) {
                Log.e("onResponse", res);
                // 将返回的json转换成实体类
                Gson gson = new Gson();
                LoginResponse loginResponse = gson.fromJson(res, LoginResponse.class);
                if(loginResponse.getCode() == 0){
                    String token = loginResponse.getToken();
                    // 将token存储到本地
                    saveStringToSp("token", token);
                    navigateToWithFlag(HomeActivity.class,
                            Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                    showToastSync("登录成功");
                }else{
                    showToastSync(loginResponse.getMsg());
                }
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.getMessage() );
            }
        });
    }

//    private void login(String account, String pwd){
//        if(StringUtils.isEmpty(account)) {
//            showToast("请输入账号");
//            return;
//        }
//        if(StringUtils.isEmpty(pwd)){
//            showToast("请输入密码");
//            return;
//        }
//
//        // 第一步创建OKHttpClient
//        OkHttpClient client = new OkHttpClient.Builder()
//                .build();
//        // 第二步创建数据map
////        Map m = new HashMap();
////        m.put("mobile", account);
////        m.put("password", pwd);
////        JSONObject jsonObject = new JSONObject(m);
////        String jsonStr = jsonObject.toString();
////        RequestBody requestBodyJson =
////                RequestBody.create(MediaType.parse("application/json;charset=utf-8")
////                    , jsonStr);
//        RequestBody requestBody = new FormBody.Builder()
//                .add("umail", account)
//                .add("upass", pwd)
//                .build();
//        // 第三步创建Request
//        Request request = new Request.Builder()
//                .url(AppConfig.Base_URL + "/app/login")
////                .addHeader("contentType", "application/json;charset=UTF-8")
//                .post(requestBody)
//                .build();
//        // 第四步创建call回调对象
//        final Call call = client.newCall(request);
//        // 第五步发起请示
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                Log.e("onFailure", e.getMessage() );
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                String result = response.body().string();
//                Log.e("onResponse", result);
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        showToast(result);
//                    }
//                });
//            }
//        });
//    }
}